﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using NPOI.HSSF.UserModel;
using OurWedding1.Models;


namespace OurWedding1.IO
{
    public class GuestGroupsExcellRenderer : IRendererable
    {

        public List<GuestGroup> Rows { get; set; }

        public void Render(Stream output)
        {
            var templateWorkbook = new HSSFWorkbook();
            templateWorkbook.CreateSheet("Arkusz 1");
            // Getting the worksheet by its name... 
            var sheet = templateWorkbook.GetSheet("Arkusz 1");

            // Getting the row... 0 is the first row. 

            var firstRow = sheet.CreateRow(0);
            firstRow.CreateCell(0);
            firstRow.GetCell(0).SetCellValue(Resources.GuestGroup_Name);
            firstRow.CreateCell(1);
            firstRow.GetCell(1).SetCellValue(Resources.GuestGroup_InvitationText);
            firstRow.CreateCell(2);
            firstRow.GetCell(2).SetCellValue(Resources.GuestGroup_GuestSideId);
            firstRow.CreateCell(3);
            firstRow.GetCell(3).SetCellValue(Resources.GuestGroup_Adress);
            firstRow.CreateCell(4);
            firstRow.GetCell(4).SetCellValue(Resources.GuestGroup_City);
            firstRow.CreateCell(5);
            firstRow.GetCell(5).SetCellValue(Resources.GuestGroup_PostCode);
            firstRow.CreateCell(6);
            firstRow.GetCell(6).SetCellValue("Klucze potwierdzenia"); //TODO:Potwierdzenie



            for (var i = 0; i < Rows.Count(); i++)
            {
                var dataRow = sheet.CreateRow(i + 1);
                dataRow.CreateCell(0);
                dataRow.GetCell(0).SetCellValue(Rows[i].Name);
                dataRow.CreateCell(1);
                dataRow.GetCell(1).SetCellValue(Rows[i].InvitationText);
                dataRow.CreateCell(2);
                dataRow.GetCell(2).SetCellValue(Rows[i].GuestSide.Name);
                dataRow.CreateCell(3);
                dataRow.GetCell(3).SetCellValue(Rows[i].Adress);
                dataRow.CreateCell(4);
                dataRow.GetCell(4).SetCellValue(Rows[i].City);
                dataRow.CreateCell(5);
                dataRow.GetCell(5).SetCellValue(Rows[i].PostCode);
                if (!Rows[i].ConfirmationKeys.Any()) continue;
                var s = "";
                foreach (var item in Rows[i].ConfirmationKeys)
                {
                    s = item.Key + "; ";
                }
                dataRow.CreateCell(6);
                dataRow.GetCell(6).SetCellValue(s);
            }


            // Forcing formula recalculation... 

            sheet.ForceFormulaRecalculation = true;
            sheet.AutoSizeColumn(0);
            sheet.AutoSizeColumn(1);
            sheet.AutoSizeColumn(2);
            sheet.AutoSizeColumn(3);
            sheet.AutoSizeColumn(4);
            sheet.AutoSizeColumn(5);
            sheet.AutoSizeColumn(6);
            // Writing the workbook content to the FileStream... 

            templateWorkbook.Write(output);
        }
    }
}